package com.example.chapter02;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class reg extends AppCompatActivity implements View.OnClickListener {
    private EditText edit_username;
    private EditText edit_phone;
    private EditText edit_pwd;
    private EditText edit_email;
    private EditText edit_sex;
    private Button button_login;
    private Button button_exit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.active_reg);
        initView();

    }

    private void initView() {
        edit_username = (EditText) findViewById(R.id.edit_username);
        edit_phone = (EditText) findViewById(R.id.edit_phone);
        edit_pwd = (EditText) findViewById(R.id.edit_pwd);
        edit_email = (EditText) findViewById(R.id.edit_email);
        edit_sex = (EditText) findViewById(R.id.edit_sex);
        button_login = (Button) findViewById(R.id.button_regist);
        button_exit = (Button) findViewById(R.id.button_exit);
        button_login.setOnClickListener(this);
        button_exit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String username = edit_username.getText().toString();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, "username不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String phone = edit_phone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "phone不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String pwd = edit_pwd.getText().toString();
        if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "pwd不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        String email = edit_email.getText().toString();
        if (TextUtils.isEmpty(email)) {
            Toast.makeText(this, "pwd不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        Bundle bundle =new Bundle();
        bundle.putString("username",username);
        bundle.putString("pwd",pwd);
        bundle.putString("phone",phone);
        bundle.putString("email",email);

        switch (v.getId()) {
            case R.id.button_regist:
                //保存到文件
                User user=new User(username,  pwd,  phone, email);
                user.saveUserDate(this);

                Intent intent =new Intent(reg.this,ShowActivity.class);
                intent.putExtra("bundle",bundle);
                startActivity(intent);
                break;
            case R.id.button_exit:
                Intent intent1 =new Intent();
                intent1.putExtra("bundle",bundle);
                setResult(102,intent1);
                //必须销毁后才返回
                Log.i("tag","销毁后才返回");
                finish();
                break;
        }
    }
    private void back() {

    }


    private void submit() {

    }
}
